package horsechess.client.handle;

import horsechess.client.data.ChessBoardData;
import horsechess.client.data.CustomizeData;
import horsechess.client.data.PlayerData;
import horsechess.client.gui.GUIChessMan;
import horsechess.client.gui.GUIClient;

import horsechess.util.Point;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * @author Administrator
 * @version 2.2
 * @created 17-Apr-2011 11:51:53 AM
 */
public class ChessBoard {

    /**
     * The information of board
     */
    private ChessBoardData informationBoard;
    private JLabel lblMessage;
    private JPanel pnlControl;
    //private GUIClient uiClient;

    public ChessBoard() {
        informationBoard = new ChessBoardData();
        if (informationBoard.players.size() == 0) {
            informationBoard.init();
        }
    }

    //Getter && Setter
    public ChessBoardData getInformationBoard() {
        return informationBoard;
    }

    public void setInformationBoard(ChessBoardData informationBoard) {
        this.informationBoard = informationBoard;
    }

    /**
     *
     * @param data
     */
    public ChessBoard(ChessBoardData data) {
        informationBoard = data;
    }

    /**
     * Destroy all element on board
     *
     * @param panel
     */
    public void destroyChessBoard(JPanel panel) {
        panel.removeAll();
    }

    /**
     *
     * @param data
     */
    public void updateDataChessBoard(CustomizeData data) {
        informationBoard.setImageBoard(data.getPathImageChessBoard());
        informationBoard.setNumberPlayers(data.getNumberOfPlayer());
        informationBoard.setDiceNumber(data.getNumberOfDice());
        //System.out.println("Data of dice on CB: "+informationBoard.getDiceNumber()+" vs Data of dice on CDATA: "+data);
    }

    /**
     *
     * @param posChessMan    The first position of chessman
     * @param i
     * @param player
     * @param panel
     */
    public void createPosOfChessManOnBoard(Point posChessMan, int i, PlayerData player) {
        Point p;
        int x = -24, y = -24;
        if (i == 1) {
            y = -y;
        }
        if (i == 2) {
            x = -x;
            y = -y;
        }
        if (i == 3) {
            x = -x;
        }
        //System.out.println("Number of chessman: "+player.getNumberOfChessMan());
        for (int j = 0; j < player.getNumberOfChessMan(); j++) {

            if (j == 0 || j == 2 || j == 4) {
                p = new Point(posChessMan.getX() + (j / 2 * x), posChessMan.getY());
            } else {
                p = new Point(posChessMan.getX() + ((j - 1) / 2 * x), posChessMan.getY() + y);
            }
            horsechess.client.handle.ChessMan cm = player.listOfChessMan.get(j);
            //System.out.println("position" + j + " :" + p.toString());
            cm.getChessman().setPositionInCage(p);
            cm.getChessman().setPositionOnBoard(-1);
            cm.getChessman().setStartBattleValue(i * 14);
            if (cm.getChessman().getStartBattleValue() == 0) {
                cm.getChessman().setPositionFinal(55);
            } else {
                cm.getChessman().setPositionFinal(cm.getChessman().getStartBattleValue() - 1);
            }


            cm.getChessman().setColorChess(i + 1);
            //System.out.println("Color: "+cm.getChessman().getColorChess());
            cm.getGUIChessMan().showUp(i + 1);
            Point position = cm.getChessman().getPositionInCage();

            cm.getGUIChessMan().picChessMan.setBounds(position.getX(), position.getY(), 40, 40);
            cm.getGUIChessMan().setChessboard(pnlControl);
            cm.getGUIChessMan().setUiClient(GUIClient.uIClient);
            //+ "#"+position.toString()
            cm.getGUIChessMan().picChessMan.setToolTipText(j + "");
            pnlControl.add(cm.getGUIChessMan().picChessMan);

        }
    }

    /**
     * Arrange chessboard luc start game
     * @param panel
     * @param data
     */
    public void arrangeChessBoard(CustomizeData data, JLabel lblMessage) {

        this.pnlControl = GUIClient.uIClient.pnlChessBoard;
        this.lblMessage = lblMessage;
        destroyChessBoard(pnlControl);
        updateDataChessBoard(data);
        int n = this.informationBoard.getNumberPlayers();
        //System.out.println("Number players: " + n);
        for (int i = 0; i < n; i++) {
            PlayerData player = informationBoard.players.get(i);
            //System.out.println("PlayerInfo: " + player.getUsername());
            if (player != null) {
                createPosOfChessManOnBoard(informationBoard.positionInCages.get(i), i, player);
            }
        }
    }

    /**
     * Remove specific player
     * @param removePlayer
     */
    public void removePlayer(String playerName) {
        List<PlayerData> players = informationBoard.players;
        for (int i = 0; i < players.size(); i++) {
            PlayerData playerData = players.get(i);
            if (playerData.getUsername().equalsIgnoreCase(playerName)) {
                informationBoard.players.set(i, null);
                List<ChessMan> chessmans = playerData.listOfChessMan;
                for (int j = 0; j < chessmans.size(); j++) {
                    //JLabel picCHessMan = chessmans.get(i).getGUIChessMan().picChessMan;
                    chessmans.get(i).getGUIChessMan().picChessMan.setBounds(-1, -1, 33, 33);
                }

            }
        }

    }

    /**
     * handle the info on chessboard
     */
    public void processChessBoard() {

        int currentUser = informationBoard.getCurrentPlayer();
        //System.out.println("Current user: " + currentUser);
        //System.out.println("Number Of Horse  :" + informationBoard.players.get(currentUser - 1).getNumberOfChessMan());
        PlayerData playerData = this.informationBoard.players.get(currentUser - 1);

        for (int i = 0; i < playerData.getNumberOfChessMan(); i++) {
            horsechess.client.handle.ChessMan cm = playerData.listOfChessMan.get(i);
            cm.getGUIChessMan().setChessMan(cm.getChessman());
            cm.getGUIChessMan().setChessBoard(informationBoard);
            cm.getGUIChessMan().setPlayer(playerData);
            cm.getGUIChessMan().setStateClick(Boolean.TRUE);
            playerData.listOfChessMan.set(i, cm);
        }

    }

    public boolean isMove() {
        boolean isMove = false;
        int currentUser = informationBoard.getCurrentPlayer();
        PlayerData playerData = this.informationBoard.players.get(currentUser - 1);
        for (int i = 0; i < playerData.getNumberOfChessMan(); i++) {
            horsechess.client.handle.ChessMan cm = playerData.listOfChessMan.get(i);
            cm.getGUIChessMan().setChessMan(cm.getChessman());
            cm.getGUIChessMan().setChessBoard(informationBoard);
            cm.getGUIChessMan().setPlayer(playerData);
            if (cm.getGUIChessMan().checkMoving() == 1 && !playerData.horseInFinals.contains(cm)) {
                isMove = true;
                //cm.getGUIChessMan().setStateClick(isMove);
            }
            //playerData.listOfChessMan.set(i, cm);
        }
        return isMove;
    }

    /**
     *
     * @param posOnChessBoard
     */
    public Point getPosition(int posOnChessBoard) {
        Point pTemp = new Point(307, 307);
        int dx = 0, dy = 0, d = 43;
        if (posOnChessBoard == 55) {
            dy = 7 * d;
        }//Yellow
        if (posOnChessBoard == 13) {
            dx = 7 * d;
        }//Red
        if (posOnChessBoard == 27) {
            dy = -7 * d;
        }//Green
        if (posOnChessBoard == 41) {
            dx = -7 * d;
        }//Blue

        if (posOnChessBoard >= 28 && posOnChessBoard <= 33) {
            dx = -d;
            dy = -d * (35 - posOnChessBoard);
        }
        if (posOnChessBoard >= 48 && posOnChessBoard <= 54) {
            dx = -d;
            dy = d * (posOnChessBoard - 47);
        }
        if (posOnChessBoard >= 0 && posOnChessBoard <= 5) {
            dx = d;
            dy = d * (7 - posOnChessBoard);
        }
        if (posOnChessBoard >= 21 && posOnChessBoard <= 26) {
            dx = d;
            dy = -d * (posOnChessBoard - 19);
        }
        if (posOnChessBoard >= 6 && posOnChessBoard <= 12) {
            dy = d;
            dx = d * (posOnChessBoard - 5);
        }
        if (posOnChessBoard >= 42 && posOnChessBoard <= 47) {
            dy = d;
            dx = -d * (49 - posOnChessBoard);
        }
        if (posOnChessBoard >= 34 && posOnChessBoard <= 40) {
            dy = -d;
            dx = -d * (posOnChessBoard - 33);
        }
        if (posOnChessBoard >= 14 && posOnChessBoard <= 20) {
            dy = -d;
            dx = d * (21 - posOnChessBoard);
        }

        pTemp.setX(pTemp.getX() + dx);
        pTemp.setY(pTemp.getY() + dy);
        return pTemp;
    }
}
